package src.Client;

import src.Server.YYChatServer;
import src.common.MessageType;
import src.common.impl.Message;

import javax.swing.*;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

public class ClientReceiverThread extends Thread{

    Socket s;

    public ClientReceiverThread(Socket s) {
        this.s=s;
    }

    public void run(){//定义线程方法

        while(true){//在线程中不断接收客户端发送来的信息

            try {

//创建对象输入流对象
                ObjectInputStream ois=new ObjectInputStream(s.getInputStream());
                Message mess=(Message) ois.readObject () ; // Message X

                if (mess.getMessageType().equals(MessageType.USER_EXIT_CLIENT_THREAD_CLOSE))
                {
                    System.out.println("关闭"+mess.getSender()+"用户的接受线程");
                    s.close();
                    break;
                }
               if (mess.getMessageType().equals(MessageType.ADD_NEW_FRIEND_FAILURE_NO_UERR)){
                   JOptionPane.showMessageDialog(null,"该名称不存在");
               }

                if (mess.getMessageType().equals(MessageType.ADD_NEW_FRIEND_FAILURE_ALREADY_EXITS)){
                    JOptionPane.showMessageDialog(null,"该用户已经是你的好友,不能重复添加!");
                }

                if (mess.getMessageType().equals(MessageType.ADD_NEW_FRIEND_SUCCESS)){
                    JOptionPane.showMessageDialog(null,"好友添加成功");
                    String sender = mess.getSender();
                    FriendList friendList = ClientLogin.hmFriendList.get(sender);

                    String allFriends = mess.getContent();
                    friendList.showAllFriends(allFriends);

                }

                if (mess.getMessageType () .equals (MessageType. COMMON_CHAT_MESSAGE) ) {
                    String receiver = mess.getReceiver();
                    String sender = mess.getSender();
                    FriendChat fc = (FriendChat) FriendList.hmFriendChat.get(receiver+"对"+sender);
                    if (fc!= null){
                        fc.append(mess);
                    }else {
                        System.out.println("请打开"+receiver+"to"+sender+"的聊天窗口");
                    }
                }

                if (mess.getMessageType () .equals (MessageType.RESPONSE_ONLINE_FRIENDS) ) {
                    FriendList fl = (FriendList) ClientLogin.hmFriendList.get(mess.getReceiver());
                    fl.activeOnlineFriendIcon(mess);
                }

                if (mess.getMessageType () .equals (MessageType.NEW_ONLINE_FRIEND) ){
                    String receiver = mess.getReceiver();
                    FriendList friendList = ClientLogin.hmFriendList.get(receiver);
                    String sender = mess.getSender();
                    friendList.activeNewOnlineFriendIcon(sender);
                }
            }catch (ClassNotFoundException | IOException e) {

                e.printStackTrace () ;

            }
        }
    }
}